package com.virjar.ratel.envmock;

import android.util.Log;


import com.virjar.ratel.allcommon.Constants;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import external.org.apache.commons.lang3.StringUtils;

@Deprecated
public class PropertiesStoreHolder {
    private File file;
    private long lastUpdate = 0;
    private Properties properties = new Properties();

    private static Map<File, PropertiesStoreHolder> storeHolderMap = new HashMap<>();

    public static synchronized PropertiesStoreHolder getPropertiesHolder(File file) {
        PropertiesStoreHolder propertiesStoreHolder = storeHolderMap.get(file);
        if (propertiesStoreHolder != null) {
            return propertiesStoreHolder;
        }
        propertiesStoreHolder = new PropertiesStoreHolder(file);
        storeHolderMap.put(file, propertiesStoreHolder);
        return propertiesStoreHolder;
    }

    //这里不能new，需要保持单例，否则加锁不成功
    private PropertiesStoreHolder(File file) {
        this.file = file;
        if (file.exists()) {
            try (FileInputStream fileInputStream = new FileInputStream(file)) {
                properties.load(fileInputStream);
                lastUpdate = file.lastModified();
            } catch (IOException e) {
                Log.w(Constants.TAG, "init properties failed ,read file: " + file.getAbsolutePath(), e);
                throw new IllegalStateException(e);
            }
        } else {
            File parentFile = file.getParentFile();
            if (!parentFile.exists()) {
                if (!parentFile.mkdirs()) {
                    throw new IllegalStateException("create dir failed: " + parentFile.getAbsolutePath());
                }
            }
        }
    }

    private void reload() {
        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            properties.load(fileInputStream);
        } catch (IOException e) {
            Log.w(Constants.TAG, "reload properties failed ,read file: " + file.getAbsolutePath(), e);
            throw new IllegalStateException(e);
        }
    }

    public synchronized boolean hasProperty(String key) {
        if (lastUpdate < file.lastModified()) {
            reload();
        }
        return properties.containsKey(key);
    }

    public synchronized String getProperty(String key) {
        //TODO cache数据一致性问题
        String cacheValue = properties.getProperty(key);
        if (cacheValue != null) {
            return cacheValue;
        }
        if (lastUpdate < file.lastModified()) {
            reload();
        }
        return properties.getProperty(key);
    }

    public synchronized void setProperty(String key, String value) {
        if (StringUtils.equals(getProperty(key), value)) {
            return;
        }
        properties.setProperty(key, value);
        try {
            properties.store(new FileOutputStream(file), "auto generated by ratel properties cache");
            lastUpdate = file.lastModified();
        } catch (IOException e) {
            Log.w(Constants.TAG, "save properties failed ,write file: " + file.getAbsolutePath(), e);
        }
    }
}
